package com.zshield.entry;

import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.zshield.util.Etime;

import java.time.LocalDateTime;

public class NetIoEntry implements EntryInterface<NetIoEntry> {
    @SerializedName("SRC_NAME")
    private String srcName;
    @SerializedName("DST_NAME")
    private String dstName;
    @SerializedName("RCV_BYTES")
    private long rcvBytes;
    @SerializedName("XMT_BYTES")
    private long xmtBytes;
    @SerializedName("TIME")
    private String time;
    @SerializedName("OPERATE_SIZE")
    private long operateSize;
    @SerializedName("DAY_TIME")
    private String dayTime;
    @SerializedName("TIME_hms")
    private String timeHms;
    @SerializedName("@timestamp")
    private String timeStamp;
    @SerializedName("FORMAT")
    private String format;
    @SerializedName("OS")
    private String os;
    @SerializedName("USERNAME")
    private String userName;
    @SerializedName("USERNAME_EX")
    private String usernameEx;
    @SerializedName("SENSOR_ID")
    private String sensorId;
    @SerializedName("PTYPE")
    private String ptype;
    @SerializedName("EXE_NAME")
    private String exeName;
    @SerializedName("FLOW_TYPE")
    private String flowType;
    @SerializedName("DST_IP")
    private String dstIp;

    public NetIoEntry(JsonObject obj) {
        srcName = obj.get("SRC_NAME").getAsString();
        dstName = obj.get("DST_NAME").getAsString();
        rcvBytes = obj.get("RCV_BYTES").getAsLong();
        xmtBytes = obj.get("XMT_BYTES").getAsLong();
        String logTime = obj.get("TIME").getAsString();
        Etime etime = new Etime();
        time = etime.zoneParse(logTime);
        operateSize = rcvBytes + xmtBytes;
        format = "PRE_COMPUTE";
        os = obj.get("OS").getAsString();
        userName = obj.get("USERNAME").getAsString();
        usernameEx = obj.get("USERNAME_EX").getAsString();
        sensorId = obj.get("SENSOR_ID").getAsString();
        exeName = obj.get("EXE_NAME").getAsString();
        flowType = obj.get("FLOW_TYPE").getAsString();
        dstIp = obj.get("DST_IP").getAsString();
        LocalDateTime ldt = LocalDateTime.parse(time, Etime.DATETIME_FORMATTER);
        dayTime = ldt.format(Etime.DATE_FORMATTER);
        timeStamp = LocalDateTime.now().format(Etime.DATETIME_FORMATTER);
        timeHms = ldt.format(Etime.TIME_FORMATTER);
        ptype = "NetIO";
    }

    public void update(NetIoEntry that) {
        this.rcvBytes = this.rcvBytes + that.rcvBytes;
        this.xmtBytes = this.xmtBytes + that.xmtBytes;
        this.operateSize = this.operateSize + that.operateSize;
        this.time = that.time;
        this.timeHms = that.timeHms;
        this.timeStamp = that.timeStamp;
    }

    @Override
    public String getDayTime() {
        return this.dayTime;
    }

    public String getDocId() {
        return "NetIO~" + srcName + "~" + dstName + "~" + os + "~" + exeName + "~" + dayTime;
    }

}
